<template>
  <view class="analyse-table">
    <view v-for="(ticket, index) in analyseTableData" :key="index">
      <view class="flex-tr">
        <view class="flex-td--label">分析时间</view>
        <view class="flex-td--value flex-center" v-for="(date, idx) in ticket.date" :key="idx">
          {{ date ? $u.timeFormat(date, 'mm 月 dd 日 hh 时 MM 分 ss 秒') : '' }}
        </view>
      </view>
      <view class="flex-tr">
        <view class="flex-td--label">分析点</view>
        <view class="flex-td--value flex-center" v-for="(content, idx) in ticket.content" :key="idx">
          {{ content }}
        </view>
      </view>
      <view class="flex-tr">
        <view class="flex-td--label">分析人</view>
        <view class="flex-td--value flex-center" v-for="(signatureUrl, idx) in ticket.signatureUrl" :key="idx">
          <image
            v-if="signatureUrl"
            :src="`${staticUrl}${signatureUrl}`"
            mode="aspectFill"
            class="signature__img"
            @click="handlePreviewImage(`${staticUrl}${signatureUrl}`)"
          ></image>
        </view>
      </view>

      <view class="flex-tr">
        <view class="flex-td--label">可燃气体检测结果</view>
        <view
          class="flex-td--value flex-center"
          v-for="(reviewResultLabel, idx) in ticket.reviewResultLabel"
          :key="idx"
        >
          {{ reviewResultLabel }}
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import { getTicketAnalyse } from '@/api/spw/ticketApply'
import { staticUrl } from '@/config'
const column = 3
export default {
  props: {
    ticketId: [Number, String]
  },
  data() {
    return {
      staticUrl,
      analyseTableData: [
        {
          date: Array(column).fill(''),
          content: Array(column).fill(''),
          analyticTargetLabel: Array(column).fill(''),
          reviewResultLabel: Array(column).fill(''),
          signatureUrl: Array(column).fill(''),
          confirmor: Array(column).fill('')
        }
      ]
    }
  },
  created() {
    getTicketAnalyse(this.ticketId).then(({ data }) => {
      const analyseData = []
      data.forEach((item) => {
        const content = JSON.parse(item.analyseContent)
        const code = item.alarmDeviceCode ? `（${item.alarmDeviceCode}）` : ''
        if (content.analyticResult === '1') {
          analyseData.push({
            ...content,
            reviewResultLabel: [
              content.analyticTargetLabel,
              content.analyticData + content.unit,
              content.analyticResultLabel + code
            ].join('，'),
            date: item.createTime,
            analyseId: item.analyseId,
            signatureUrl: item.signatureUrl
          })
        }
      })

      const result = []
      let n = 0
      let idx = 0
      analyseData.forEach((item, index) => {
        if (index % column === 0) {
          const len = result.push({
            date: Array(column).fill(''),
            content: Array(column).fill(''),
            analyticTargetLabel: Array(column).fill(''),
            reviewResultLabel: Array(column).fill(''),
            signatureUrl: Array(column).fill(''),
            confirmor: Array(column).fill('')
          })
          idx = len - 1
          n = 0
        }
        result[idx].date[n] = item.date
        result[idx].content[n] = item.content
        result[idx].analyticTargetLabel[n] = item.analyticTargetLabel
        result[idx].reviewResultLabel[n] = item.reviewResultLabel
        result[idx].signatureUrl[n] = item.signatureUrl
        result[idx].confirmor[n] = item.confirmor
        n++
      })
      this.analyseTableData = result
    })
  },
  methods: {
    handlePreviewImage(url) {
      uni.previewImage({
        urls: [url]
      })
    }
  }
}
</script>
<style scoped lang="scss">
@import './style.scss';
</style>
